from flask import Blueprint, Flask, Response
from chatbot_graph import *
from flask import request
from flask_cors import CORS
import json

bpQuery = Blueprint("query", __name__, url_prefix='/query')

handler = ChatBotGraph()  # 提高回复速度
# r'/*' 是通配符，让本服务器所有的 URL 都允许跨域请求
CORS(bpQuery, resources=r'/*')


@bpQuery.route('/question', methods=["GET", "POST"])
def Query():
    if request.method == 'GET':
        ques = request.args.get('content', '')
        ans = handler.chat_main(ques)
        dic = {"question": ques, "answer": ans}
        tmpStr = str(dic).replace("\\h", "")
        tmpStr = tmpStr.replace("\\xa0", "")
        json_str = tmpStr.replace("'", "\"")
        return Response(json.dumps(json.loads(json_str)), mimetype='application/json')  # return json_str  # 单返回这个好像也可以
    elif request.method == 'POST':
        return 'post'


@bpQuery.route('/fans')
def Getfans():
    return '我是蓝图离谱'
